/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui.widgets.visual.results;

import java.awt.Color;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import dgbgui.gui.widgets.Widget;
import dgbgui.gui.widgets.WidgetFactory;
import dgbgui.gui.widgets.layouts.GridLayout;
import dgbgui.gui.widgets.visual.LabelWidget;
import dgbgui.gui.widgets.visual.RectangleBorder;
import dgbgui.modeldocument.QueryResult;
import dgbgui.modeldocument.primitives.Primitive;
import edu.umd.cs.piccolo.PNode;

@SuppressWarnings("serial")
public class ResultsWidget extends RectangleBorder {

    
    public ResultsWidget(PNode parent, QueryResult result) {
        // Top flat, bottom rounded
        super(parent,0.0,0.0);
        
        m_resultWidgets = new LinkedHashSet<ResultWidget>();
        
        // Create a layout for the result items
        m_layout = new GridLayout(m_layout,2.0,0.0,5.0);
        
        // For each of the results in the query result,
        // create a widget according to the type

        Map<Primitive,Widget> widgetMap = new HashMap<Primitive,Widget>();
        for ( Primitive primitive : result.getPrimitives() ) {

            ResultWidget widget = WidgetFactory.createWidget(m_layout,primitive);
            
            if ( widget != null) {
                m_resultWidgets.add(widget);
                widgetMap.put(primitive,widget);
            }
        }
        
        // Empty result
        if ( result.getPrimitives().size() == 0 ) {
            new LabelWidget(m_layout,"No result");
        }
        
        setStrokeColor(Color.black);
        setFillColor(getDefaultFillColour());
    }
    

    public Set<ResultWidget> getResultWidgets() {
        return m_resultWidgets;
    }
    
    
    // The result widgets
    LinkedHashSet<ResultWidget> m_resultWidgets;

}
